package com.grab.page.utils;

import org.springframework.util.ObjectUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class CrawlerUtils {
    private static String UTF8_CHARSET = "utf-8";
    private static String GBK_CHARSET = "gbk";

    /**
     * 获取访问网站的数据
     * @param params 参数
     * @param cookieVal cookie的值
     * @param url url路径
     * @param method
     * @return response的值
     */
    public static String getContent(String params, String cookieVal, String url,String method,String charset) {
        StringBuffer sb = null;
        try {
            String content = params;
            URL urls = content == null ? new URL(url) : new URL(url + content);
            HttpURLConnection urlconnection = (HttpURLConnection) urls.openConnection(); // 打开连接
            urlconnection.setDoOutput(true);
            urlconnection.setDoInput(true);
            urlconnection.setRequestMethod(method);
            urlconnection.setUseCaches(false);
            urlconnection.setInstanceFollowRedirects(true);
            urlconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            urlconnection.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36"); // 有些浏览器会查看浏览器信息，防止爬取。
            urlconnection.setRequestProperty("Cookie", cookieVal);
            urlconnection.connect();
            if (params != null) {
                DataOutputStream out = new DataOutputStream(urlconnection.getOutputStream());
                out.writeBytes(content);
                out.flush();
                out.close();
            }
            InputStream ins = urlconnection.getInputStream(); // 以流的方式读出内容

            BufferedReader buff = new BufferedReader(new InputStreamReader(ins,charset)); // 以字符流读入
            sb = new StringBuffer(); // 把内容提取出来
            String lines = "";
            while ((lines = buff.readLine()) != null) {
                sb.append(lines + "\n");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static String getCharset(String params, String cookieVal, String url,String method) {
        String charset = UTF8_CHARSET;
        try {
            String content = params;
            URL urls = content == null ? new URL(url) : new URL(url + content);
            HttpURLConnection urlconnection = (HttpURLConnection) urls.openConnection(); // 打开连接
            urlconnection.setDoOutput(true);
            urlconnection.setDoInput(true);
            urlconnection.setRequestMethod(method);
            urlconnection.setUseCaches(false);
            urlconnection.setInstanceFollowRedirects(true);
            urlconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            urlconnection.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36"); // 有些浏览器会查看浏览器信息，防止爬取。
            urlconnection.setRequestProperty("Cookie", cookieVal);
            urlconnection.connect();
            if (params != null) {
                DataOutputStream out = new DataOutputStream(urlconnection.getOutputStream());
                out.writeBytes(content);
                out.flush();
                out.close();
            }
            InputStream ins = urlconnection.getInputStream(); // 以流的方式读出内容

            String contentType = urlconnection.getContentType();

            Pattern pattern = Pattern.compile("charset=\\S*$");
            Matcher matcher = pattern.matcher(contentType);
            if (matcher.find()) {
                String charset_ = matcher.group().replace("charset=", "");
                if(!ObjectUtils.isEmpty(charset_)){
                    charset = charset_;
                }
            }
            charset = getCharset(ins,charset);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return charset;
    }

    public static String getCharset(InputStream ins,String charset) throws IOException {
        int isGarbledcodee_Num = 0;
        BufferedReader buff = new BufferedReader(new InputStreamReader(ins,charset)); // 以字符流读入
        String lines = "";
        while ((lines = buff.readLine()) != null) {
            if(MessyCodeCheckUtils.isGarbledcodee(lines)){
                isGarbledcodee_Num +=1;
            }
        }
        if(isGarbledcodee_Num>30 && charset==UTF8_CHARSET){
            return GBK_CHARSET;
        }else{
            return charset;
        }
    }

}
